package com.google.code.ptrends.catparsers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.google.code.ptrends.catparsers.utils.RegexHelper;
import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.common.entities.Property;
import com.google.code.ptrends.common.entities.Session;
import com.google.code.ptrends.entities.ETLItem;
import com.google.code.ptrends.entities.ParametersStrings;
import com.google.code.ptrends.excel.ExcelReader;
import com.google.code.ptrends.excel.entities.Worksheet;

public class SoundCardCategoryParser extends AbstractCategoryParser {

	private List<Matcher> matcher;

	public SoundCardCategoryParser(final ExcelReader reader,
			final Worksheet worksheet, final Session session) {
		super(reader, worksheet, session);
		matcher = new ArrayList<Matcher>();
		matcher.add(Pattern.compile(RegexHelper.getRegex("sound1")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("sound2")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("sound3")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("sound4")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("sound5")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("sound6")).matcher(""));
	}

	@Override
	protected ETLItem createItem(final String description, final String priceUSD) {
		ETLItem item = null;

		if (StringUtils.isNotBlank(description)
				&& StringUtils.isNotBlank(priceUSD)) {

			for (Matcher m : matcher) {
				m.reset(description);
				if (m.find()) {

					final java.util.List<Property> property = new ArrayList<Property>();
					item = buildItem(Category.SOUNDCARD, m.group(1),
							m.group(2), new Float(priceUSD), new Date());

					// Interface
					if (StringUtils.isNotBlank(m.group(3))) {
						property.add(buildProperty(ParametersStrings.INTERFACE,
								m.group(3)));
					}
					// Input
					if (StringUtils.isNotBlank(m.group(4))) {
						property.add(buildProperty(ParametersStrings.INPUT,
								m.group(4)));
					}

					// Output
					if (StringUtils.isNotBlank(m.group(5))) {
						property.add(buildProperty(ParametersStrings.OUTPUT,
								m.group(5)));
					}

					item.setParameters(property);

					return item;
				}
			}
		}
		return item;
	}
}
